﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using Frame.Lib.Core.Base;
using Cms.Entity.Article;
using Cms.DAL.Article;
using Frame.Lib.Web;
using Cms.BLL.Menu;
using Cms.Entity.Menu;
using Frame.Lib.Core.Attach;
using Cms.DAL;

namespace Cms.BLL.Article
{
    public class ArticleBO : CommonOperate<ArticleInfo>
    {
        private ArticleDAL _article;
        protected override BaseDAL DAL
        {
            get
            {
                return _article;
            }
        }
        
        public ArticleBO()
        {
            _article = new ArticleDAL();
            
        }

        //public List<ArticleInfo> GetList(PagerSortSetting pager)
        //{
        //    string condition = _article.GetPagerCondition( pager.Input.ToString());
        //    if (pager.SortExpression == null || pager.SortExpression == "")
        //    {
        //        pager.SortExpression = "Orders";
        //        pager.SortDirection = SortDirection.desc;
        //    }
        //    List<ArticleInfo> list = base.GetList(pager, condition, "%" + pager.Input + "%");
            
           

        //    return list;
        //}

     

        public IEnumerable<ArticleInfo> GetList(int menuID, PagerSetting pager,string articleName)
        {
            MenuBO menu = new MenuBO();
            List<int> subIDs = menu.GetSubIDs(menuID);

            IEnumerable<ArticleInfo> list = null;

            if (pager != null)
            {
                list = _article.GetListByMenu(subIDs, articleName);
                pager.RecordCount = list.Count();

                list = list.Skip(pager.SkipCount).Take(pager.PageSize);
            }
            else
            {
                list = _article.GetListByMenu(subIDs, null);
            }

            return list;
        }

        /// <summary>
        /// 在前台能够显示的文章
        /// </summary>
        /// <param name="menuID"></param>
        /// <param name="pager"></param>
        /// <returns></returns>
        public IEnumerable<ArticleInfo> GetDisplayList(int menuID, PagerSetting pager)
        {
            IEnumerable<ArticleInfo> list = this.GetList(menuID, pager,null).Where(t => t.IsDisplay == true);
            return list;
        }

        public IEnumerable<ArticleInfo> GetDisplayListByCode(int code, PagerSetting pager)
        {
            MenuBO menu = new MenuBO();
            int id = (int)menu.GetIDByCode(code);
            return GetDisplayList(id, pager);
        }


        public void MoveMenu(List<int> articleIDs, int menuID)
        {
            _article.MoveMenu(articleIDs, menuID);
        }

        /// <summary>
        /// 取出前台需要显示的几条记录
        /// </summary>
        /// <param name="menuID"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IEnumerable<ArticleInfo> GetDisplayList(int menuID, int recordCount)
        {
            MenuBO menu = new MenuBO();
            List<int> subIDs = menu.GetSubIDs(menuID);

            List<ArticleInfo> list = _article.GetDisplayList(subIDs, recordCount);
            foreach (ArticleInfo info in list)
            {
                SetDelegate(info);
            }
            return list;
        }

        public IEnumerable<ArticleInfo> GetDisplayListByCode(int code, int recordCount)
        {
            MenuBO menu = new MenuBO();            
            int id = (int)menu.GetIDByCode(code);
            return GetDisplayList(id, recordCount);
        }

        /// <summary>
        /// 在前台能够显示的文章
        /// </summary>
        /// <param name="menuID"></param>
        /// <returns></returns>
        public List<ArticleInfo> GetDisplayList(int menuID)
        {
            MenuBO menu = new MenuBO();
            List<int> subIDs = menu.GetSubIDs(menuID);
            List<ArticleInfo> list = _article.GetDisplayList(subIDs, null);
            return list;
        }

        public IEnumerable<ArticleInfo> GetDisplayListByCode(int code)
        {
            MenuBO menu = new MenuBO();
            int id = (int)menu.GetIDByCode(code);
            return GetDisplayList(id);
        }


        public ArticleInfo GetFirstInfo(int menuID)
        {
            int? id = _article.GetFirstID(menuID);
            if (id != null)
            {
                return this.GetInfo(id);              
            }
            else
            {
                return null;
            }
        }

        public override ArticleInfo GetInfo(object id)
        {
            ArticleInfo info = base.GetInfo(id);

            if (id != null)
            {                
                SetDelegate(info);
                _article.SetPictureCount(new List<ArticleInfo> { info });             
            }

           
            if (info.GetMenuInfo == null)
            {
                info.GetMenuInfo = menuID => (new MenuBO()).GetInfo(menuID);
            }
           

            return info;
        }

        public void SetDelegate(ArticleInfo info)
        {
            ImageBO picture = new ImageBO(CTableName.ArticlePicture);
            AttachBO attach = new AttachBO(CTableName.ArticleAttach);

            info.GetPictures = articleID => picture.GetList(articleID, null);
            info.GetAttachs = articleID => attach.GetList(articleID);
           
            info.GetMenuInfo = menuID => (new MenuBO()).GetInfo(menuID);
        }

        public override void Delete(IEnumerable<int> ids)
        {
            foreach (int id in ids)
            {
                this.Delete(id);
            }
        }

        public override void Delete(int id)
        {
            AttachBO attach = new AttachBO(CTableName.ArticleAttach);
            ImageBO picture = new ImageBO(CTableName.ArticlePicture);

            this.DeleteTitlePicture(id);  

            attach.DeleteByBindID(id);

            picture.DeleteByBindID(id);

            base.Delete(id);

        }

        public void DeleteByMenuIDs(IEnumerable<int> menuIDs)
        {
            IEnumerable<int> ids = _article.GetIDsByMenuIDs(menuIDs);
            this.Delete(ids);
        }

        public void DeleteTitlePicture(object id)
        {
            ArticleInfo info = this.GetInfo(id);
            if (!string.IsNullOrEmpty(info.SmallImage))
            {
                string phyPath = System.Web.HttpContext.Current.Server.MapPath(info.SmallImageAbsPath);
                if (File.Exists(phyPath))
                {
                    File.Delete(phyPath);
                }

                phyPath = System.Web.HttpContext.Current.Server.MapPath(info.BigImageAbsPath);

                if (File.Exists(phyPath))
                {
                    File.Delete(phyPath);
                }
            }

        }

        /// <summary>
        /// 根据附件的ID把文章的ID取出来，主要用于删除
        /// </summary>
        /// <param name="attachIDs"></param>
        /// <returns></returns>
        public List<int> GetIDsByAttachIDs(List<int> attachIDs)
        {
            return _article.GetIDsByAttachIDs(attachIDs);

        }
   

        public override void Save(ArticleInfo info)
        {
            base.Save(info);
            
            //_article.SaveMenu(info);
            SetDelegate(info);
        }

         /// <summary>
        /// 获取每个文章的第一个附件
        /// </summary>
        /// <param name="menuID"></param>
        /// <returns></returns>
        public List<AttachInfo> GetAttachList(int menuID)
        {
            List<AttachInfo> list = _article.GetAttachList(menuID);
            return list;
        }


        /// <summary>
        /// 保存CmsArticlePicture中的图片
        /// </summary>
        /// <param name="pictureInfo"></param>
        /// <param name="file"></param>
        /// <param name="menuInfo"></param>
        public void SaveImage(ImageInfo pictureInfo, System.Web.HttpPostedFile file, MenuInfo menuInfo)
        {
            UserImage userImage = new UserImage(CTableName.ArticlePicture);

            if (menuInfo.PicSaveOrignImage)
            {
                pictureInfo.OriginImage = userImage.Save(file); //先存原始图片
            }

            if (menuInfo.PicSallImageSize > 0)
            {
                pictureInfo.SmallImage = userImage.Save(file, menuInfo.PicSallImageSize);
            }

            if (menuInfo.PicBigImageSize > 0)
            {
                pictureInfo.BigImage = userImage.Save(file, menuInfo.PicBigImageSize);
            }

        }

        /// <summary>
        /// 保存CmsArticle中的图片
        /// </summary>
        /// <param name="info"></param>
        /// <param name="httpPostedFile"></param>
        public void SaveImage(ArticleInfo info, System.Web.HttpPostedFile file)
        {
            this.SetDelegate(info);
            MenuInfo menuInfo = info.MenuInfo;
            UserImage userImage = new UserImage(CTableName.Article);
            if (menuInfo.ArticleSmallImageSize > 0)
            {
                info.SmallImage = userImage.Save(file, menuInfo.ArticleSmallImageSize);
            }

            if (menuInfo.ArticleBigImageSize > 0)
            {
                info.BigImage = userImage.Save(file, menuInfo.ArticleBigImageSize);
            }

        }



       
    }
}
